package com.milkTeaServer.controller;

import com.milkTeaServer.bean.User;
import com.milkTeaServer.dao.UserDao;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@RestController
@RequestMapping("/user")
public class UserController {
    @Autowired
    UserDao dao;

   /* @RequestMapping("/login")
    @ResponseBody
    public Map login(@RequestBody User user) {
        Map map = new HashMap();
        System.out.println(user.toString());
        User user1 = dao.findOne(Example.of(user)).orElse(null);
        if (user1 != null) {
            map.put("success", true);
            map.put("massage", "登录成功");
            map.put("data", user1);
        } else {
            map.put("success", false);
            map.put("massage", "登录失败");
            map.put("data", null);
        }
        return map;
    }*/

    @RequestMapping("/login")
    @ResponseBody
    public Map login(@RequestBody User user) {
        Map map = new HashMap();
        List<User> users = dao.selectUser(user.getName(), user.getPassword());
        User user1 = users.get(0);
        System.out.println(user1);
        if (user1 != null) {
            map.put("success", true);
            map.put("massage", "登录成功");
            map.put("data", user1);
        } else {
            map.put("success", false);
            map.put("massage", "登录失败");
            map.put("data", null);
        }
        return map;
    }

    @RequestMapping("/register")
    @ResponseBody
    public Map register(@RequestBody User user) {
        Map map = new HashMap();
        User user1 = dao.saveAndFlush(user);
        if (user1 != null) {
            map.put("success", true);
            map.put("massage", "注册成功");
        } else {
            map.put("success", false);
            map.put("massage", "注册失败");
        }
        return map;
    }


    @RequestMapping("/update")
    @ResponseBody
    public Map updateAll(@RequestBody User user) {
        Map map = new HashMap();

        int i = dao.updateEmailById(user.getEmail(), user.getId());
        int i1 = dao.updatePasswordById(user.getPassword(), user.getId());
        int i2 = dao.updatePhoneById(user.getPhone(), user.getId());
        int i3 = dao.updateSexById(user.getSex(), user.getId());

        if (i != 0 && i1 != 0 && i2 != 0 && i3 != 0) {
            map.put("success", true);
            map.put("massage", "更新资料成功");
        } else if (i == 0) {
            map.put("success", false);
            map.put("massage", "更新资料失败");
        }
        return map;
    }
    

    //查余额
    @RequestMapping("/money")
    @ResponseBody
    public Map idByMoney(@RequestBody User user) {
        Map map = new HashMap();
        int i = dao.idByMoney(user.getMoney(), user.getId());
        if (i != 0) {
            map.put("success", true);
            map.put("massage", "查余额成功");
            map.put("data", user);
        } else if (i == 0) {
            map.put("success", false);
            map.put("massage", "查余额失败");
            map.put("data", null);
        }
        return map;
    }

    //充值
    @RequestMapping("/recharge")
    @ResponseBody
    public Map recharge(@RequestBody User user) {
        Map map = new HashMap();
        int i = dao.recharge(user.getMoney(), user.getId());
        if (i != 0) {
            map.put("success", true);
            map.put("massage", "充值成功");
        } else if (i == 0) {
            map.put("success", false);
            map.put("massage", "充值失败");
        }
        return map;
    }

    @RequestMapping("/findById")
    @ResponseBody
    public Map findUserByID(@RequestBody User user) {
        Map map = new HashMap();
        User user1 = dao.findUserById(user.getId()).get(0);
        if (user1 != null) {
            map.put("success", true);
            map.put("massage", "查询user成功");
            map.put("data", user1);
        } else {
            map.put("success", false);
            map.put("massage", "查询user失败");
            map.put("data",null);
        }
        return map;
    }
}
